/**
 * 宽带预约列表
 */
(function($) {
  var pageData = {};
  var pageCount = 1;
  var filterType = 3;
  var pageNumber = 0;
  var opponentList = new Array();
  /*
  封装参数
  */
  var getMyParams = function(id) {
    var params = "?";
    var myParam = getParams();
    console.log("myParam=" + myParam);
    if (myParam) {
      params += myParam;
      if (id) {
        params += "&";
      }
    }
    if (id) {
      params += "id=" + id;
    }
    console.log("params=" + params);
    return params;
  };

  var bind = function(object, clickFunc) {
    itemOnClick(object, clickFunc);
  }

  var itemOnClick = function(object, clickFunc) {
    $(object).off("click"); //取消li事件
    /*li事件*/
    $(object).on("click", function() {
      var $siblingsNode = $(this).siblings();
      var primaryClass = "select-item";
      var defaultColor = "#fff";
      var selectColor = "#0c79b1";

      if ($siblingsNode.hasClass(primaryClass)) {
        $siblingsNode.removeClass(primaryClass);
        $siblingsNode.css("background-color", defaultColor);
      }
      if ($(this).hasClass(primaryClass)) {
        $(this).removeClass(primaryClass);
        $(this).css("background-color", defaultColor);
      } else {
        $(this).addClass(primaryClass);
        $(this).css("background-color", selectColor);
      }
      clickFunc(this);
      event.stopPropagation();
    });
  }


  var groupClickFunc = function(object) {
      var id = $(object).find("input[name='opponent_id']").val();
      getMyParams(id);
      window.location.href = "opponent_detail.html" + getMyParams(id);
    }
    /*
    显示数据
    */
  var showOpponentData = function(data) {
    if (data instanceof Array) {
      list = data;
      for (var i = 0; i < list.length; i++) {
        opponentList.push(list[i]);
      }
    } else if (data instanceof Object) {
      opponentList.push(data.groupInfoList);
    }
    var $tpl = $('#amz-group-list');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
      list: opponentList,
      "error": "没有集团信息"
    };

    $("#opponent-list").children('li').remove();
    $("#opponent-list").children('div').remove();

    var html = template(myData);
    $tpl.before(html);
    bind("#opponent-list > li", groupClickFunc);
  };


  var loadOpponentData = function(pageNumber, operator) {
    showLoadingBar($("#loadingbar"), true);
    groupNo = getParamValue("group_no");
    console.log("groupNo=" + groupNo);
    $.ajax({
      type: "post",
      contentType: "application/json",
      // data: JSON.stringify(params),
      url: "../../../opponent/getOpponentList?group_name=&group_no=" + groupNo + "&operator=" + operator,
      dataType: "json",
      async: true,
      success: function(data) {
        showLoadingBar($("#loadingbar"), false);
        console.log("data = " + data);
        pageData = data.resultCode;
        console.info(data.resultCode);
        if (data.resultCode != 1000) {
          console.log("获取RPC接口返回失败！code:" + data.rtnCode);
          return;
        }
        if (data.resultCode != 1000) {
          console.log("获取信息接口返回失败！code:" + data.return.resultCode);
          return;
        }
        showOpponentData(data.data);
      },
      error: function(textStatus) {
        showLoadingBar($('#loadingbar'), false);;
        console.info("request error:");
      }
    });
  };

  $(function() {
    /*滚动条*/
    var $doc = $(document);
    $doc.scroll(function() {
      console.log("height = " + ($doc.height() - $(window).height()));
      console.log("scrollTop = " + Math.ceil($doc.scrollTop()));
      console.log("Math.ceil($doc.scrollTop()) = " + Math.ceil($doc.scrollTop()));
      if (Math.ceil($doc.scrollTop()) >= ($doc.height() - $(window).height())) {
        var $msg = $("#no-data-msg");
        console.log("debug 1");
        if (!$.isEmptyObject(pageData)) {
          console.log("debug 2");
          if (!$msg.hasClass("am-hide")) {
            console.log("debug 3");
            $msg.addClass("am-hide");
          }
          console.log("pageNumber=" + pageNumber);
          pageNumber++;
          loadGroupData(pageNumber);
        } else {
          console.log("debug 5");
          if ($msg.hasClass("am-hide")) {
            console.log("debug 6");
            $msg.removeClass("am-hide");
          }
        }
      }
    });


    $("#search_btn").on("click", function() {
      operator = $("#searchType").val();
      console.log("operator="+operator);
      if(!operator ){
        $("#search_content").html("查询条件：所有");
      }else{
        $("#search_content").html("查询条件："+operator);
      }
      console.log("operator=" + operator);
      if (operator == "所有") {
        operator = "";
      }
      pageNumber = 0;
      opponentList = [];
      loadOpponentData(pageNumber, operator);
    });
    $("#search").on("click", function() {
      $('#search-modal').modal('open');
    });
    $("#add-btn").on("click", function() {
      console.log("add btn clicked");
      window.location.href = "idc_add.html?" + getParams();
    });
    $("#search_content").html("查询条件：所有");
    loadOpponentData(pageNumber, "");
  });
})(jQuery);